var Promise = require('es6-promise.min.js');

function wxPromisify(fn) {
  return function (obj = {}) {
    return new Promise((resolve, reject) => {
      obj.success = function (res) {
        //成功
        resolve(res)
      }
      obj.fail = function (res) {
        //失败

        reject(res)
      }
      fn(obj)
    })
  }
}

Promise.prototype.finally = function (callback) {
  let P = this.constructor;
  return this.then(
    value => P.resolve(callback()).then(() => value),
    reason => P.resolve(callback()).then(() => { throw reason })
  );
};



function getRequest(url, data) {
  var getRequest = wxPromisify(wx.request)
  return getRequest({
    url: url,
    method: 'GET',
    data: data,
    
  })
}

function postRequest(url, data) {
  var postRequest = wxPromisify(wx.request)
  return postRequest({
    url: url,
    method: 'POST',
    data: data,
    
  })
}

function wxLogin() {
  return wxPromisify(wx.login)
}

function wxGetUserInfo() {
  return wxPromisify(wx.getUserInfo)
}

function wxMakePhoneCall(phoneNumber) {
  var wxMakePhoneCall = wxPromisify(wx.makePhoneCall)
  return wxMakePhoneCall({
    phoneNumber: phoneNumber,
  })
}
function wxcheckSession() {
  return wxPromisify(wx.checkSession)
}

module.exports = {
  wxPromisify: wxPromisify,
  postRequest: postRequest,
  getRequest: getRequest,
  wxLogin: wxLogin,
  wxGetUserInfo: wxGetUserInfo,
  wxMakePhoneCall: wxMakePhoneCall,
  wxcheckSession: wxcheckSession
}
